package lk.icta.core.dao.impl;

import lk.icta.common.AppConstants;
import lk.icta.common.ErrorCodes;
import lk.icta.common.dao.PreloadableDAO;
import lk.icta.common.dao.impl.BaseDAO;
import lk.icta.core.dao.GeologicalTypeDAO;
import lk.icta.core.entity.GeologicalType;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Supun Nimesh Karunathilaka
 * @author Duminda Dharamakeerthi
 */
public class GeologicalTypeDAOImpl extends BaseDAO implements GeologicalTypeDAO, PreloadableDAO {

    private final Map<Integer, String> siNames = new HashMap<Integer, String>();
    private final Map<Integer, String> enNames = new HashMap<Integer, String>();
    private final Map<Integer, String> taNames = new HashMap<Integer, String>();

    @Transactional(propagation = Propagation.MANDATORY)
    public void addGeologicalType(GeologicalType geologicalType, User user) {
        logger.debug("Adding a GeologicalType [Geological Type: {}\tUser: {}]", geologicalType.getId(), user.getUsername());
        em.persist(geologicalType);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void updateGeologicalType(GeologicalType geologicalType, User user) {
        logger.debug("Updating a GeologicalType [Geological Type: {}\tUser: {}]", geologicalType.getId(), user.getUsername());
        em.merge(geologicalType);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public GeologicalType getGeologicalTypeById(int id, User user) {
        logger.debug("Loading GeologicalType [GeologicalType Id: {}]", id);
        return em.find(GeologicalType.class, id);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public Map<Integer, String> getAllGeologicalTypes(String language, User user) {
        logger.debug("Listing geological types in {}", language);
        Map<Integer, String> result = null;
        if (AppConstants.SINHALA.equals(language)) {
            result = siNames;
        } else if (AppConstants.ENGLISH.equals(language)) {
            result = enNames;
        } else if (AppConstants.TAMIL.equals(language)) {
            result = taNames;
        } else {
            handleException("Unsupported language : " + language, ErrorCodes.INVALID_LANGUAGE);
        }
        if (result != null)
            return result;
        else
            return Collections.emptyMap();
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public void preload() {
        Query query = em.createQuery("SELECT g FROM GeologicalType g");
        List<GeologicalType> results = query.getResultList();
        for (GeologicalType g : results) {
            siNames.put(g.getId(), g.getSiTypeName());
            enNames.put(g.getId(), g.getEnTypeName());
            taNames.put(g.getId(), g.getTaTypeName());
        }
    }
}
